package com.xywl.escort.view.order.subscription;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.DefaultResp;
import com.xywl.escort.bean.DeliveryApplyReq;
import com.xywl.escort.bean.ProjectSpecResp;
import com.xywl.escort.bean.RankingListReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class DeliveryDialogPresenter implements DeliveryDialogContract.Presenter {
    private DeliveryDialogContract.View mView;
    private ApiServer mApiServer;

    @Inject
    DeliveryDialogPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(DeliveryDialogContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadSpec(String projectId) {
        RankingListReq req = new RankingListReq();
        req.projectId = projectId;
        mApiServer.bigList(req)
                .compose(RxUtil.<ProjectSpecResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ProjectSpecResp>bindToLife())
                .subscribe(new BaseSubscriber<ProjectSpecResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadSpecFailure();
                    }

                    @Override
                    public void onNext(ProjectSpecResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.loadSpecSuccess(resp.data);
                        } else {
                            mView.loadSpecFailure();
                        }
                    }
                });
    }

    @Override
    public void loadAddress() {
        BaseReq req = new BaseReq();
        mApiServer.addrDefault(req)
                .compose(RxUtil.<DefaultResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<DefaultResp>bindToLife())
                .subscribe(new BaseSubscriber<DefaultResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadAddressFailure();
                    }

                    @Override
                    public void onNext(DefaultResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadAddressSuccess(resp);
                        } else {
                            mView.loadAddressFailure();
                        }
                    }
                });
    }

    @Override
    public void delivery(DeliveryApplyReq req) {
        mApiServer.deliveryApply(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.deliveryFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.deliverySuccess(resp);
                            } else {
                                mView.deliveryFailure(resp.msg);
                            }
                        } else {
                            mView.deliveryFailure("操作失败");
                        }
                    }
                });
    }


    @Override
    public void unSubscribe() {
        Timber.tag("DeliveryDialogPresenter").e("unSubscribe");
        mView = null;
    }
}
